package com.ljj.examisto.application

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import com.ljj.examisto.R
import com.ljj.examisto.notification.NotificationChannelManager

class Examisto : Application() {
    private lateinit var prefs: SharedPreferences
    private val prefsName by lazy { getString(R.string.app_preference_name) }
    private val keyFirstLaunch by lazy { getString(R.string.key_is_first_launch) }
    private val keyIs24HourFormat by lazy { getString(R.string.key_is_24_hour_format) }

    override fun onCreate() {
        super.onCreate()
        initNotificationChannels()
        initPreferences()
        checkFirstLaunch()
    }

    private fun initNotificationChannels() {
        NotificationChannelManager.createChannels(this)
    }

    private fun initPreferences() {
        prefs = getSharedPreferences(prefsName, Context.MODE_PRIVATE)
    }

    private fun checkFirstLaunch() {
        if (prefs.getBoolean(keyFirstLaunch, true)) {
            initializeDefaultSettings()
            setFirstLaunchCompleted()
        }
    }

    private fun initializeDefaultSettings() {
        prefs.edit().apply {
            putBoolean(keyIs24HourFormat, true)
            apply()
        }
    }

    private fun setFirstLaunchCompleted() {
        prefs.edit()
            .putBoolean(keyFirstLaunch, false)
            .apply()
    }
}